<template>
	<view class="index">
		<view class="products u-flex u-flex-wrap u-row-between">
			<view 
				class="item" 
				v-for="item,index of products" 
				:key="index"
				@click="toDetail(item)"
			>
				<view class="iamge">
					<u-image 
						:src="item.main_image_cdn"
						:lazy-load="true"
						height="338"
						width="340"
					></u-image>
				</view>
				<view class="info u-flex-col">
					<view class="title">
						<text>{{item.title}}</text>
					</view>
					<view class="footer u-flex u-col-center u-row-between">
						<text class="price">￥{{item.price}}</text>
						<text class="addr">青海</text>
					</view>
				</view>
			</view>
		</view>
		<u-empty :show="products.length == 0"></u-empty>
		<u-loadmore :status="loadStatus" v-show="products.length > 0"></u-loadmore>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				loadStatus: 'loadmore',
				products: [],
				categoryId: null
			};
		},
		methods: {
			loadProducts(){
				return this.$api.product.index({
					page: this.page,
					category: this.categoryId
				})
				.then(res=>{
					if(res.code == 1 && res.data.last_page >= this.page){
						this.products = this.products.concat(res.data.data)
					}else{
						this.loadStatus = 'nomore'
					}
				})
			},
			reload(item){
				this.page = 1
				this.loadmore = 'loadmore'
				this.products = []
				this.categoryId = item.id
				return this.loadProducts()
			},
			nextPage(){
				if(this.loadStatus == 'nomore') return
				this.page = this.page + 1
				return this.loadProducts()
			},
			toDetail(item){
				this.$Router.push({
					name: 'product.detail',
					params: {
						id: item.id
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.index{
		background-color: $uni-bg-color;
		.products{
			padding: $uni-spacing-col-base $uni-spacing-row-base;
			.item{
				margin-bottom: $uni-spacing-col-base;
				background-color: #F6F6F6;
				border-radius: 6upx;
				.info{
					padding: $uni-spacing-col-base 8upx;
					.title{
						font-size: 26upx;
						color: #333333;
						font-weight: bold;
						padding-bottom: 24upx;
					}
					.footer{
						.price{
							color: #E12E2E;
							font-size: 34upx;
							font-weight: bold;
						}
						.addr{
							font-size: 24upx;
							color: #969696;
						}
					}
				}
			}
		}
	}
</style>
